home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 2510.ZIP / TRSOURCE.EXE / FILEREAD.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  3KB  |  84 lines

  1. /*********
  2. *  Syntax: FILEREAD( [<drive>:][\<path>]<filename.ext> )
  3. *  By: Tom Rettig, Leonard Zerman
  4. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  5. *
  6. *  Return: <expC> contents of a disk text file.
  7. *          <expC> error message if any trouble.
  8. *********/
  9. #include "trlib.h"
  10.  
  11. TRTYPE fileread()
  12. {
  13.    static char funcname[] = { "fileread" };
  14.    char *filename, *ret, *tempret;
  15.    int filenum;
  16.    int i, status;
  17.    int readbytes;
  18.    long filebytes, maxbytes;
  19.    long holdbytes;
  20.  
  21.    if ( PCOUNT==1 && ISCHAR(1) )
  22.    {
  23.       filename = _parc(1);
  24.       filenum  = _tr_open( filename, READONLY );
  25.       if ( filenum != FILEERROR && *filename )
  26.       {
  27.          filebytes = _tr_lseek( filenum, 0L, 2 );         /* eof */
  28.          if ( filebytes != (long)FILEERROR )
  29.          {
  30.             maxbytes = filebytes;
  31.             holdbytes = maxbytes;
  32.             ret = _tr_allocmem((unsigned)maxbytes+1);   /* +1 for nullc */
  33.             if ( ret && maxbytes < 65535)
  34.             {
  35.                _tr_lseek( filenum, 0L, 0 );               /* position to bof */
  36.  
  37.                i = 0;
  38.                tempret = ret;
  39.                while (maxbytes > 32767L)
  40.                {
  41.                   if (status = _tr_read( filenum, tempret, 32767) <= 0)
  42.                   {
  43.                      _retc( _tr_errmsgs(funcname,E_FREAD) );
  44.                      _tr_close( filenum );
  45.                      _tr_freemem( ret,(unsigned)holdbytes+1 );
  46.                   }
  47.                   tempret += 32767;
  48.                   maxbytes -= 32767L;
  49.                }
  50.                readbytes = _tr_read( filenum, tempret, maxbytes );
  51.                if ( readbytes != (long)FILEERROR )
  52.                {
  53.                   ret[(unsigned)readbytes-1] = NULLC; /* replace eof with nullc */
  54.                   _retc( ret );
  55.                   _tr_close( filenum );
  56.                   _tr_freemem( ret,(unsigned)holdbytes+1 );
  57.                }
  58.                else
  59.                {
  60.                   _retc( _tr_errmsgs(funcname,E_FREAD) );
  61.                   _tr_close( filenum );
  62.                   _tr_freemem( ret, (unsigned)holdbytes+1 );
  63.                }
  64.             }
  65.             else
  66.             {
  67.                _retc( _tr_errmsgs(funcname,E_ALLOC) );
  68.                _tr_close( filenum );
  69.             }
  70.          }
  71.          else
  72.          {
  73.             _retc( _tr_errmsgs(funcname,E_FSEEK) );
  74.             _tr_close( filenum );
  75.          }
  76.       }
  77.       else
  78.          _retc( _tr_errmsgs(funcname,E_FOPEN) );
  79.    }
  80.    else
  81.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  82. }
  83. /* eof fileread */
  84.